﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.UnionPay.Response
{
    [Serializable]
    class RefundNotifyResponseEntity
    {
        /// <summary>
        /// 退款订单号
        /// </summary>
        public string orderId { get; set; }

        /// <summary>
        /// 签名方法 signMethod  必填 非对称签名： 01（表示采用RSA签名） HASH表示散列算法 11：支持散列方式验证SHA-256 12：支持散列方式验证SM3  
        /// </summary>
        public string signMethod { get; set; }

        /// <summary>
        /// 签名 signature ANS1..1024 M 填写对报文摘要的签名，可通过SDK生成签名 
        /// </summary>
        public string signature { get; set; }

        /// <summary>
        /// 清算币种
        /// </summary>
        public string settleCurrencyCode { get; set; }

        /// <summary>
        /// 流水号
        /// </summary>
        public string queryId { get; set; }

        /// <summary>
        /// 跟踪号
        /// </summary>
        public string traceNo { get; set; }

        /// <summary>
        /// 清算日期
        /// </summary>
        public string settleDate { get; set; }

        /// <summary>
        /// 交易传输时间
        /// </summary>
        public string traceTime { get; set; }

        /// <summary>
        /// 交易金额
        /// </summary>
        public long? txnAmt { get; set; }

        /// <summary>
        /// 清算金额
        /// </summary>
        public long settleAmt { get; set; }

        /// <summary>
        /// 应答码
        /// </summary>
        public string respCode { get; set; }
        /// <summary>
        /// 应答消息
        /// </summary>
        /// 
        public string respMsg { get; set; }

        /// <summary>
        /// 退款结果
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public ITradeRefundApplyResult ToRefundResult(ITradeChannel channel)
        {
            if (string.IsNullOrWhiteSpace(this.orderId))
            {
                channel.ThrowChannelValidationException("银联返回的流水号为null或空白。");
            }
            if (this.txnAmt == null || this.txnAmt <= 0)
            {
                channel.ThrowChannelValidationException("银联退款通知总金额小于或等于零。");
            }
            var value = Convert.ToDecimal(this.txnAmt.Value) / 100M;
            if (respCode.Equals("00"))
            {
                return new TradeRefundApplyResult(channel, this.orderId, queryId, value, 0m,
                    DateTimeUtils.GetDateTimeFormat(this.traceTime, "MMddHHmmss"),
                    TradeApplyResultState.Complete_Success, "退款成功");
            }
            return new TradeRefundApplyResult(channel, this.orderId, queryId, value, 0m, null, TradeApplyResultState.Apply_Error, respMsg);

        }
    }
}
